package com.park.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.park.entity.BlackList;
import com.park.service.BlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlackListController {
    @Autowired
    private BlackListService bls;

    @GetMapping("blacklist.html")
    public String login() {
        return "blacklist";
    }

    //分页查所有
    @GetMapping("/parking/listblack")
    @ResponseBody
    public List<BlackList> blacklistList(@RequestParam(value="current",required=false)int current){
        IPage<BlackList> page= bls.selectPage(current);
        return page.getRecords();
    }

    //分页总数
    @GetMapping("/parking/blacklisttotalpage")
    @ResponseBody
    public  long totalPage(){
        IPage<BlackList> page= bls.selectPage(1);
        long total=page.getTotal()/page.getSize();
        if(page.getTotal()%page.getSize()!=0){
            total +=1;
        }
        return total;
    }

    //模糊查询
    @GetMapping("/parking/searchblaclist")
    @ResponseBody
    public List<BlackList> searchByCarId(@RequestParam(value = "carId", required = false) String carId,@RequestParam(value="current",required=false)int current) {
        return bls.getByBlackListCarId(carId,current);
    }

    //模糊查询总页数
    @GetMapping("/parking/blacklisttotalpage2")
    @ResponseBody
    public  long totalPage2(@RequestParam(value = "carId", required = false) String carId){
        int page= bls.getBlackListPage(carId);
        long total=page/6;
        if(page%6!=0){
            total +=1;
        }
        return total;
    }

    //删除用户
    @GetMapping("/parking/blacklistDel")
    @ResponseBody
    public String blacklistIdDel(HttpSession session, @RequestParam("blacklistId")int blacklistId) {
        int result=bls.delBlackList(blacklistId);
        Map<String,String> map=new HashMap<String,String>();
        if(result==1) {
            map.put("delResult","true");
        }else {
            map.put("delResult","false");
        }
        return JSON.toJSONString(map);

    }
}
